#-------------------------------------------------------------------------------
# Name:        Player
# Purpose:
#
# Author:      Michael Blackwood
#
# Created:     23/10/2013
# Copyright:   (c) Monxcleyr Productions 2013
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import pygame, logging
import sprite
from ...tools import subpixelsurface

class Player(sprite.Sprite):


  def __init__(self, objtype, name, attributes, imagehandler):
    sprite.Sprite.__init__(self, objtype, name, attributes, imagehandler)
    self.ancestors.append('player')
    self.attributes['speed'] = float(self.attributes['speed'])

    # Holds if the use key is currently pressed
    self.attributes['use'] = False
    self.attributes['blocked'] = False



  def update(self, common):

    """ Update player object """
    key = common.keyState.pressed

    if not self.attributes['blocked']:
      self.attributes['use'] = False
      if common.keyState[common.key.bind['use']][0]:
        self.attributes['use'] = True

      self._moveupdate(common)

  def _moveupdate(self, common):

    key = common.keyState.pressed

    move = (0,0)
    speed = self.getMoveSpeed(common.frametime)

    # Check for input
    if key[common.key.bind['left']]:
      move = (move[0] - speed, move[1])
    if key[common.key.bind['right']]:
      move = (move[0] + speed, move[1])
    if key[common.key.bind['up']]:
      move = (move[0], move[1] - speed)
    if key[common.key.bind['down']]:
      move = (move[0], move[1] + speed)

    if move[0] != 0 and move[1] != 0:
      move = (move[0] * .75, move[1] * .75)

    if move[0] < 0 and move[1] == 0:
      self.attributes['frame'] = 3
    if move[0] > 0 and move[1] == 0:
      self.attributes['frame'] = 1
    if move[0] < 0 and move[1] < 0:
      self.attributes['frame'] = 4
    if move[0] > 0 and move[1] > 0:
      self.attributes['frame'] = 6
    if move[0] == 0 and move[1] < 0:
      self.attributes['frame'] = 0
    if move[0] == 0 and move[1] > 0:
      self.attributes['frame'] = 2
    if move[0] > 0 and move[1] < 0:
      self.attributes['frame'] = 5
    if move[0] < 0 and move[1] > 0:
      self.attributes['frame'] = 7

    # move sprite
    self.move(move)

  def getMoveSpeed(self, time):

    """ Returns the move speed for the current frame, based on time """

    movepermilli = self.attributes['speed'] / 1000.0
    speed =  time * movepermilli

    # Throttle the speed here. Tiles are 16x16, moving farther than 8 between a collision
    # check could cause the player to slip through the tile.
    if speed > 8:
      speed = 8

    return speed



  def draw(self, surface, offset, common):

    """ Player has it's own draw method, so we can use subpixel on it, where normal sprites do not."""

    if self.attributes['visable']:

      if common.settings.quality == 3:
        # Subpixel drawing
        sprite = self.imagehandler.getSprite(self.attributes['sprite'])
        sprite_sub = pygame.Surface(self.attributes['framesize']).convert_alpha()
        sprite_sub.fill((0,0,0,0))
        pos = self.attributes['pos']
        pos = (pos[0] + offset[0], pos[1] + offset[1])
        frames = self.attributes['frames']
        frame = self.attributes['frame']
        sprite_sub.blit(sprite, (0,0), frames[frame])
        sprite_sub = subpixelsurface.SubPixelSurface(sprite_sub, x_level=3)

        surface.blit(sprite_sub.at(pos[0], pos[1]), pos)
      else:
        # normal drawing
        sprite = self.imagehandler.getSprite(self.attributes['sprite'])
        pos = self.attributes['pos']
        pos = (pos[0] + offset[0], pos[1] + offset[1])
        frames = self.attributes['frames']
        frame = self.attributes['frame']

      rect = surface.blit(sprite, pos, frames[frame])
      return rect





